import Vue from 'vue'
import { Dialog,Toast   } from 'vant';
    
/* @desc:不拦截的请求名 */
const unInterceptorUrl = []

//不弹loading的请求名
const unLoadingUrl = ['subject/list']



/* @desc:检测请求名是否需要拦截 */
function checkUrl(resp){
    let arr = resp.config.url.split('/'),flag = true  
    unInterceptorUrl.forEach(item => {
        if (item === arr[arr.length - 2] + '/' + arr[arr.length - 1]) {
            flag = false
        }
    })
    return flag
}

/* @desc:检测不弹loading的请求名*/
function checkLoadUrl(resp){
    let arr = resp.url.split('/'),flag = true
    unLoadingUrl.forEach(item => {
        if (item === arr[arr.length - 2] + '/' + arr[arr.length - 1]) {
            flag = false
        }
    })
    return flag
}



export const request = function(config){
    if(checkLoadUrl(config)){
        Toast.loading({
            forbidClick: true, // 禁用背景点击
            message: '拼命加载中...'
        });
    }
    return config
}

export const response = function(response){
    Toast.clear();
    if(response.data.code == 404){
        Dialog.alert({
            title: '错误消息',
            message: '程序正在修复中'
          })
    }
    if(response.data.code!=200&&checkUrl(response)){
        Dialog.alert({
            title: '提示',
            message: response.data.msg
          })
    }
    return response   
}